<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.yaodian100.core.common.utils.ServerValue"%>
<LINK rel="stylesheet" type="text/css" href="/css/jquery.tooltip.css">
<script language="javascript">
var cpId = "<%=request.getParameter("cpId") %>";
var cpPrice = "<%=request.getParameter("cpPrice") %>";
var cpRemind = "<%=request.getParameter("cpRemind") %>";
var ccId = "<%=request.getParameter("ccId") %>";
	$().ready(function(){
		//收藏商品
		$("#pdialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height:150,
			width:350,
			modal:true
		});
	
		//收藏类别
		$("#cdialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height:255,
			width:450,
			modal: true
		});

		$("#restock_remind_dialog").dialog({
			bgiframe: true,
			width:300,
			modal: true,
			autoOpen: false
		});

		$("#restock_remind_exist_dialog").dialog({
			bgiframe: true,
			width:300,
			modal: true,
			autoOpen: false
		});
		
		$("#c_memo").focus(function(){
			if($("#c_memo").val()=='(您可以输入简单的备注，最多50个字。)'){
			$("#c_memo").val("").css("color","#000")};
		}).blur(function(){
			if($("#c_memo").val()==''){
			$("#c_memo").val("(您可以输入简单的备注，最多50个字。)").css("color","#aaa")};
		});
		
		if( cpId!="null" && cpPrice!="null"){
			if(cpRemind == "Y"){
				saveRestockRemindProduct(cpId,cpPrice);
			}else{
				saveWishProduct(cpId,cpPrice);
			}
		}else if(ccId != "null"){
			checkWishCategoryExist(ccId);
		}
	
		$("#d_ok").click(function(){$("#pdialog").dialog('close');});
		$("#rr_ok").click(function(){$("#restock_remind_dialog").dialog('close');});
		$("#rre_ok").click(function(){$("#restock_remind_exist_dialog").dialog('close');});
		$("#cd_ok").click(function(){
			saveWishCategories(ccId, $("#c_memo").val()=="(您可以输入简单的备注，最多50个字。)"?"":$("#c_memo").val());
			$("#cdialog").dialog('close');
		});
		$("#cd_cancel").click(function(){$("#cdialog").dialog('close');});
		
		//收藏商品
		$(":a[id*=addWishList]").click(function(){
			cpId = $(this).attr('id').split('_')[1];
			cpPrice = $(this).attr('id').split('_')[2];
			$.post("<%=ServerValue.getFrontContextPath()%>/checkLogin.do",{pathparam:'<%=ServerValue.getFrontHostname()%>'+window.location.pathname+"?<%=request.getQueryString()%>&cpId="+cpId+"&cpPrice="+cpPrice},callback,"json");
			function callback(result){
				if(result=='login'){
					$.post("/ecmall/nonMember/checkLogin.do",{},saveWishNonmemberCheckResultP,"json");
				}else{
					window.location.href = "<%=ServerValue.getFrontContextPath()%>/tologin.do?redirect_url="+result; 
				}
			}
		});

		//补货通知
		$(":a[id*=addRestockRemind]").click(function(){
			cpId = $(this).attr('id').split('_')[1];
			cpPrice = $(this).attr('id').split('_')[2];
			cpRemind = "Y";
			$.post("<%=ServerValue.getFrontContextPath()%>/checkLogin.do",{pathparam:'<%=ServerValue.getFrontHostname()%>'+window.location.pathname+"?<%=request.getQueryString()%>&cpId="+cpId+"&cpPrice="+cpPrice+"&cpRemind=Y"},callback,"json");
			function callback(result){
				if(result=='login'){
					$.post("/ecmall/nonMember/checkLogin.do",{},saveWishNonmemberCheckResultP,"json");
				}else{
					window.location.href = "<%=ServerValue.getFrontContextPath()%>/tologin.do?redirect_url="+result; 
				}
			}
		});
		
		//收藏类别
		$(":a[id*=addCategoriesList]").click(function(){
			ccId = $(this).attr('id').split('_')[1];
			$.post("<%=ServerValue.getFrontContextPath()%>/checkLogin.do",{pathparam:'<%=ServerValue.getFrontHostname()%>'+window.location.pathname+"?<%=request.getQueryString()%>&ccId="+ccId},callback,"json");
			function callback(result){
				if(result=='login'){
					$.post("/ecmall/nonMember/checkLogin.do",{},saveWishNonmemberCheckResultC,"json");
				}else{
					window.location.href = "<%=ServerValue.getFrontContextPath()%>/tologin.do?redirect_url="+result; 
				}
			}
		});

		$("#c_memo").keyup(function(){
			var text = $(this).attr('id').split('_')[1];
	        checklength("c_memo",50);
	    });
	    
	});
	//收藏商品
	function saveWishProduct(productId, productPrice){
			$.post("<%=ServerValue.getFrontContextPath()%>/member/myCollection/saveWishList.do",{productId:productId,productPrice:productPrice},callback,"json");
			function callback(result){
				$("#pdialog").attr("class","collection-dailog-ok ui-dialog-content ui-widget-content");
				if(result=='success'){
					$("#result").text('此商品已成功加入收藏！');
				}else{
					$("#result").text('此商品已存在於收藏盒中！');
				}
				$("#tocollection").attr('href',"<%=ServerValue.getFrontContextPath()%>/member/myCollection/toproductc.do");
				$("#pdialog").dialog('open');
				$("#pdialog").dialog( "option", "title", '收藏商品' );
			}
	}

	//補貨通知
	function saveRestockRemindProduct(productId, productPrice){
		$.post("<%=ServerValue.getFrontContextPath()%>/member/myCollection/saveRestockRemind.do",{productId:productId,productPrice:productPrice},callback,"json");
		function callback(result){
			if(result=='success'){
				$("#restock_remind_dialog").dialog('open');
			}else{
				$("#restock_remind_exist_dialog").dialog('open');
			}			
		}
	}
	
	//检查收藏类别是否已存在
	function checkWishCategoryExist(categoryId){
		$.post("<%=ServerValue.getFrontContextPath()%>/member/myCollection/checkWishList.do",{categoryId:categoryId},
				function callback(result){
					if(result=='exists'){
						$("#result").text('此分类已存在於收藏盒中！');	
						$("#tocollection").attr('href',"<%=ServerValue.getFrontContextPath()%>/member/myCollection/tocategoriesc.do");
						$("#pdialog").dialog('open');		
					}else{
						$("#cdialog").dialog('open');
						$("#c_memo").blur();
					}
				}
			,"json");
	}
	
	//收藏类别
	function saveWishCategories(categoryId, categoryMemo){
		$.post("<%=ServerValue.getFrontContextPath()%>/member/myCollection/saveWishList.do",{categoryId:categoryId, categoryMemo:categoryMemo},callback,"json");
		function callback(result){
			if(result=='success'){
				$("#result").text('此分类已成功加入收藏');
			}else{
				$("#result").text('此分类已存在於收藏盒中！');
			}
			$("#tocollection").attr('href',"<%=ServerValue.getFrontContextPath()%>/member/myCollection/tocategoriesc.do");
			$("#pdialog").dialog('open');
			$("#pdialog").dialog( "option", "title", '收藏分类' );
		}
	}

	function checklength(ctlid,maxlength)
	{              
	   if ($("#"+ctlid).val().length > maxlength) {
	          $("#"+ctlid).val($("#"+ctlid).val().substring(0, maxlength));
	          return false;
	   }
	}

	function saveWishNonmemberCheckResultP(result){
		if(result=='login'){
			if(cpRemind == "Y"){
				saveRestockRemindProduct(cpId,cpPrice);
			}else{
				saveWishProduct(cpId,cpPrice);
			}
		}else{
			showNonmemberDialog(result);
		}
	}

	function saveWishNonmemberCheckResultC(result){
		if(result=='login'){
			checkWishCategoryExist(ccId);
		}else{
			showNonmemberDialog(result);
		} 
	}
	
	
</script>
<div id="pdialog" title="收藏商品" class="collection-dailog-ok" style="display:none;">        
	<p id="result">此商品已成功加入收藏！</p>
	<div align="center">
		<span style="text-align:center;"><a href="<%=ServerValue.getFrontContextPath()%>/member/myCollection/toproductc.do" class="btn-mid" id="tocollection">查看收藏盒</a></span>
		<span style="text-align:center;"><a href="javascript:;" id="d_ok" class="btn-mid">确定</a></span>
	</div>
</div>
<div id="cdialog" title="收藏分类" class="collection-dailog" style="display:none;">    
	<div class="collection-dailog">
  	<p>将感兴趣的类别加入收藏，系统会将分类路径储存在「收藏盒」中，方便你下次迅速找查。</p>
    <ul>
    	<li class="w-a"><strong>分 类：</strong>${categoryPath}</li>
      	<li class="w-b">
      		<div class="f-lt"><strong>备 注：</strong><br /><span class="t-note">非必填</span></div>
	      	<div class="t-area"><textarea cols="50" rows="3" class="txt-gy" id="c_memo"></textarea></div>
      	</li>
    </ul>
  </div>
  <div align="center">
		<span style="text-align:center;"><a href="javascript:;" class="btn-mid" id="cd_ok">确定加入</a></span>
    <span style="text-align:center;"><a href="javascript:;" class="btn-mid" id="cd_cancel">取消</a></span>
  </div>
</div>

<div id="restock_remind_dialog" title="補貨通知" class="dailog-ok" style="display:none;">
<p id="restock_remind_p">设定成功！<br />商品已自动加入收藏盒，<br />货到时系统会立即以email通知您</p>
<div align="center"><a href="<%=ServerValue.getFrontContextPath()%>/member/myCollection/toproductc.do" class="btn-mid" onfocus="blur()">查看收藏盒</a> <a href="javascript:;" id="rr_ok" class="btn-mid" onfocus="blur()">确定</a></div>
</div>

<div id="restock_remind_exist_dialog" title="補貨通知" class="dailog-ok" style="display:none;">
<p>本商品您已经设定过到货通知！</p><p>&nbsp;</p>
<div align="center"><a href="<%=ServerValue.getFrontContextPath()%>/member/myCollection/toproductc.do" class="btn-mid" onfocus="blur()">查看收藏盒</a> <a href="javascript:;" id="rre_ok" class="btn-mid" onfocus="blur()">确定</a></div>
</div>